#include "task.hpp"
#include "taskManager.hpp"
#include "graph/graph.hpp"
#include <chrono>
#include <iostream>
#include <mutex>
#include <atomic>
#include <sstream>
#include <iostream>
#include "log.h"

void Task::do_task(TaskManager* tm) {
    bool expeat = false;
    if(node->visited.compare_exchange_weak(expeat,true)) {
        std::stringstream ss;
        ss << "Thread " << tm->getId() << ": " << "processing " << node->nodeID;
        LOG(tm->getOutput(), ss.str())

        tm->addProcessNum();
        node->setVisited();
        work();
        ExtendNode(tm);
    }
}

void Task::work() {
    // 自旋，平均4096次会使得work占比70%。
    for(int i = 0; i < node->dealTime * 2048; i++) {
        __asm__ __volatile__ ("rep;nop" ::: "memory");
    }
}

void Task::ExtendNode(TaskManager* tm){
    for (Edge *edge = node->firstEdge; edge != NULL; edge = edge->next) {
        if (!edge->target->isVisited()) {
            tm->push(new Task(edge->target));
        }
    }
}

Task::~Task() {}
